package com.btex.app;

import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.btex.app.adapter.MyFragmentAdapter;
import com.btex.app.entity.OTCInfoList;
import com.btex.app.pager.MyOrderAllFragment;
import com.btex.app.pager.MyOrderCancelFragment;
import com.btex.app.pager.MyOrderLockFragment;
import com.btex.app.pager.MyOrderPayFragment;
import com.btex.app.pager.MyOrderSussesFragment;
import com.btex.app.tools.DensityUtil;
import com.btex.app.treaty.dialog.OTCSetDialog;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyOrderActivity extends BaseActivity implements OTCSetDialog.getValueLIstener {

    @BindView(R.id.iv_back)
    ImageView mIvBack;
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.tv_menu)
    TextView mTvMenu;
    @BindView(R.id.rl_title)
    RelativeLayout mRlTitle;
    @BindView(R.id.otc_activity_my_order)
    TabLayout mOtcActivityMyOrder;
    @BindView(R.id.otc_order_viewpager)
    ViewPager mOtcOrderViewpager;
    @BindView(R.id.tv_otc_la)
    ImageView mTvOtcLa;
    private ArrayList<String> mStrings;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_order);
        ButterKnife.bind(this);
        mTvOtcLa.setVisibility(View.VISIBLE);
        mTvTitle.setText("USDT_CNY订单");
        initView();
        getOTCList();
    }

    private void getOTCList() {
        //        获取otc交易对
        int num = (int) ((Math.random() * 9 + 1) * 100000);
        OkGo.<String>get(Config.URL + "pubapi1/get_otc_markets?return_format=2" + "?rnd=" + String.valueOf(num)).tag(this).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                String result = response.body().toString();
                Log.i("skehewihiefhuwiuehf", result);

                try {
                    JSONObject jsonObject = new JSONObject(result);

                    String code = jsonObject.getString(Config.KEY_CODE);

                    if ("1".equals(code)) {

                        Gson gson = new Gson();
                        OTCInfoList otcInfoList = gson.fromJson(result, OTCInfoList.class);

                        mStrings = new ArrayList<>();
                        for (int i = 0; i < otcInfoList.getData().size(); i++) {
                            String pair = otcInfoList.getData().get(i).getPair();
                            mStrings.add(pair);
                        }


                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFinish() {
                super.onFinish();

            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);

            }
        });
    }

    private void initView() {
        ArrayList<Fragment> fragments = new ArrayList<>();
        ArrayList<String> strings = new ArrayList<>();
        strings.add("未付款");
        strings.add("已付款");
        strings.add("已锁定");
        strings.add("已取消");
        strings.add("已完成");
        fragments.add(new MyOrderAllFragment());//未付款
        fragments.add(new MyOrderPayFragment());//已付款
        fragments.add(new MyOrderLockFragment());//已锁定
        fragments.add(new MyOrderCancelFragment());//已取消
        fragments.add(new MyOrderSussesFragment());//已完成

        mOtcOrderViewpager.setAdapter(new MyFragmentAdapter(getSupportFragmentManager(), fragments, strings));
        mOtcOrderViewpager.setOffscreenPageLimit(4);


        mOtcActivityMyOrder.setSelectedTabIndicatorHeight(5);
        mOtcActivityMyOrder.setupWithViewPager(mOtcOrderViewpager);
    }

    @Override
    public void setData() {

    }

    @Override
    public void onClick(View v) {

    }

    @OnClick({R.id.iv_back, R.id.tv_otc_la, R.id.tv_title})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_otc_la:
//                showZhiBiao(this, mRlTitle);
                break;

            case R.id.tv_title:
//                showZhiBiao(this, mRlTitle);
                if (mStrings.size() > 0) {
                    OTCSetDialog otcSetDialog = new OTCSetDialog(this, mStrings);
                    otcSetDialog.getValue(this);
                    otcSetDialog.show();
                }

                break;
        }
    }

    private void showZhiBiao(Context cxt, View view) {


        View contentView = View.inflate(cxt, R.layout.otc_la_pop, null);
        TextView pop_otc_la_tyd = (TextView) contentView.findViewById(R.id.pop_otc_la_tyd);
        TextView pop_otc_la_usdt = (TextView) contentView.findViewById(R.id.pop_otc_la_usdt);

        //2、通过Resources获取
        DisplayMetrics dm = getResources().getDisplayMetrics();
        int heigth = dm.heightPixels;
        int width = dm.widthPixels;

        final PopupWindow pop = new PopupWindow(contentView, width, 500);
        int[] location = new int[2];
        view.getLocationInWindow(location);
        pop.setOutsideTouchable(true);
//        pop.setBackgroundDrawable(cxt.getResources().getDrawable(R.color.transparent));
        pop.showAtLocation(view, Gravity.RIGHT | Gravity.TOP, DensityUtil.dip2px(cxt, 72), location[1] + DensityUtil.dip2px(cxt, 60));
        pop_otc_la_usdt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTvTitle.setText("USDT/CNY订单");
                EventBus.getDefault().post("Orderusdt");
                pop.dismiss();

            }
        });

        pop_otc_la_tyd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTvTitle.setText("TYD/TYZ订单");
                EventBus.getDefault().post("Ordertyd");
                pop.dismiss();
            }
        });

    }

    @Override
    public void get(String data) {
        mTvTitle.setText(data + "订单");
        EventBus.getDefault().post(data);
    }
}
